package com.qf.czh.vhr.Controller;

import com.qf.czh.vhr.Service.DepartmentService;
import com.qf.czh.vhr.entity.Department;
import com.qf.czh.vhr.entity.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/basic/department")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;
    @GetMapping("/")
    public List<Department> getAllDeps(){
        return departmentService.getAllDeps();
    }

    @GetMapping("/all")
    public List<Department> getAllDepsWithoutChildren() {
        return departmentService.getAllDepsWithoutChildren();
    }

    @PostMapping("/")
    public RespBean addDepartment(@RequestBody Department department){
        departmentService.addDepartment(department);

        if (department.getResult() == 1){
            return  RespBean.ok("添加成功",department);
        }else {
            return  RespBean.error("添加失败");
        }
    }

    @DeleteMapping("/{id}")
    public  RespBean deleteDepartmentById(@PathVariable Integer id){
        Department department = new Department();
        department.setId(id);
        departmentService.deleteDepartmentById(department);

        if (department.getResult()==-2){
            return RespBean.error("有子部门");
        }else if (department.getResult()==-1){
            return RespBean.error("有员工");
        }else if (department.getResult()==1){
            return RespBean.ok("删除成功");
        }else {
            return RespBean.error("删除失败");
        }

    }
}
